from typing import Dict, Any, Optional
from mem0 import Memory
from config.config import get_doubao_seed_config, get_embedding_config


def _create_memory_client(config: Optional[Dict[str, Any]] = None) -> Memory:
    """创建配置好的Memory客户端"""
    # 加载配置
    doubao_config = get_doubao_seed_config()
    embedding_config = get_embedding_config()
    
    # 默认配置
    default_config = {
        "vector_store": {
            "provider": "chroma",
            "config": {
                "collection_name": "memory",
                "path": "./data/memory/chroma_db"
            }
        },
        "llm": {
            "provider": "openai",
            "config": {
                "model": doubao_config.model,
                "api_key": doubao_config.api_key,
                "openai_base_url": doubao_config.base_url,
                "temperature": 0.2
            }
        },
        "embedder": {
            "provider": "openai",
            "config": {
                "model": embedding_config.model,
                "api_key": embedding_config.api_key,
                "openai_base_url": embedding_config.base_url
            }
        }
    }
    
    # 合并用户配置
    final_config = {**default_config, **(config or {})}
    
    # 返回配置好的客户端
    return Memory.from_config(final_config)


# 全局MemoryClient，供其他模块直接使用
MemoryClient = _create_memory_client()